package com.menghanjun.mhjresourcesharing.controller;


import com.menghanjun.mhjresourcesharing.entity.TUser;
import com.menghanjun.mhjresourcesharing.service.ITUserService;
import com.menghanjun.mhjresourcesharing.utils.JsonResponseBody;
import com.menghanjun.mhjresourcesharing.utils.JsonResponseStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author menghanjun
 * @since 2023-08-11
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class TUserController {

    @Autowired
    private ITUserService userService;

    /**
     * 增加一个用户
     * @param user
     * @return
     */
    @PostMapping("/admin/addUser")
    public JsonResponseBody<?> addUser(TUser user){
        return userService.addUser(user);
    }


    /**
     * 删除用户
     * @param userId
     * @return
     */
    @PostMapping("/admin/delUser")
    public JsonResponseBody<?> delUser(String userId){
        return userService.deleteUser(Long.parseLong(userId));
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/userLogin")
    public JsonResponseBody<?> userLogin(TUser user){
        return userService.userLogin(user);
    }

    /**
     * 获取用户列表
     * @param userId
     * @param userName
     * @param email
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/admin/getUserList")
    public JsonResponseBody<?> getUserList(String userId,String userName,String email,int pageNo, int pageSize){
        try {
            TUser user = new TUser();
            if (!ObjectUtils.isEmpty(userId)) {
                user.setUserId(Long.parseLong(userId));
            }
            user.setUserName(userName);
            user.setEmail(email);
            return userService.getUserList(user,pageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResponseBody<>(JsonResponseStatus.ERORR);
        }
    }

    /**
     * 获取一个用户(不包含角色和密码)
     * @param userId
     * @return
     */
    @GetMapping("/getOneUser")
    public JsonResponseBody<?> getOneUser(@RequestParam String userId){
        try {
            return userService.getUserById(Long.parseLong(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResponseBody<>(JsonResponseStatus.ERORR);
        }
    }

    /**
     * 获取一个用户(包含角色和密码)
     * @param userId
     * @return
     */
    @GetMapping("/admin/getOneUserAdmin")
    public JsonResponseBody<?> getOneUserAdmin(@RequestParam String userId){
        try {
            return userService.getUserByIdAdmin(Long.parseLong(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResponseBody<>(JsonResponseStatus.ERORR);
        }
    }


    @PostMapping("/exitLogin")
    public JsonResponseBody<?> exitLogin(String userId){
        try {
            return userService.exitLogin(Long.parseLong(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResponseBody<>(JsonResponseStatus.ERORR);
        }
    }

    /**
     * 编辑用户
     * @param user
     * @return
     */
    @PostMapping("/admin/editUser")
    public JsonResponseBody<?> editUser(TUser user){
        try {
            return userService.editUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResponseBody<>(JsonResponseStatus.ERORR);
        }
    }

    /**
     * 获取修改邮箱的验证码
     * @param userid
     * @return
     */
    @GetMapping("/getUserEmailCode")
    public JsonResponseBody<?> getUserEmailCode(String userid,String email){
        try {
            return userService.getUserCode(Long.parseLong(userid),email,"editemail");
        }catch (Exception e) {
            e.printStackTrace();
            return new JsonResponseBody<>(JsonResponseStatus.OPERATION_FAILED);
        }
    }


    /**
     * 获取修改密码的验证码
     * @param userid
     * @return
     */
    @GetMapping("/getUserpwdCode")
    public JsonResponseBody<?> getUserpwdCode(String userid,String email){
        try {
            return userService.getUserCode(Long.parseLong(userid),email,"editpassword");
        }catch (Exception e) {
            e.printStackTrace();
            return new JsonResponseBody<>(JsonResponseStatus.OPERATION_FAILED);
        }
    }


    /**
     * 更新密码
     * @param param
     * @return
     */
    @PostMapping(value = "/editUserPassWord" ,consumes = MediaType.APPLICATION_JSON_VALUE)
    public JsonResponseBody<?> editUserPassWord(@RequestBody Map<String,Object> param){
        try {
            int code = Integer.parseInt(param.get("code").toString());
            long userid = Long.parseLong(param.get("userid").toString());
            String password = param.get("password").toString();
            return userService.editUserPassWord(userid,code,password);
        }catch (Exception e) {
            e.printStackTrace();
            return new JsonResponseBody<>(JsonResponseStatus.OPERATION_FAILED);
        }
    }

    /**
     * 更新邮箱
     * @param param
     * @return
     */
    @PostMapping(value = "/editUserEmail" ,consumes = MediaType.APPLICATION_JSON_VALUE)
    public JsonResponseBody<?> editUserEmail(@RequestBody Map<String,Object> param){
        try {
            int code = Integer.parseInt(param.get("code").toString());
            long userid = Long.parseLong(param.get("userid").toString());
            String email = param.get("email").toString();
            return userService.editUserEmail(userid,code,email);
        }catch (Exception e) {
            e.printStackTrace();
            return new JsonResponseBody<>(JsonResponseStatus.OPERATION_FAILED);
        }
    }


    /**
     * 判断账号是否过期
     * @param userid
     * @return
     */
    @GetMapping("/verifyAccountExpiration")
    public JsonResponseBody<?> verifyAccountExpiration(String userid){
        boolean b = false;
        try {
            b = userService.verifyAccountExpiration(userid);
        }catch (Exception e) {
            log.error("verifyAccountExpiration报错！"+e.getMessage());
            e.printStackTrace();
            return new JsonResponseBody<>(JsonResponseStatus.ERORR);
        }
        return new JsonResponseBody<>(b);
    }

}
